<template>
  <div class="page-field">
     暂时只支持系统内支付

    <div class="task-pay-item">
      <div class="task-pay-title">
        您需支付金额为：
      </div>
      <div class="task-pay-value">
        {{ taskPay.total }}
      </div>
    </div>

    <div class="task-pay-item">
      <div class="task-pay-title">
        账户余额为：
      </div>
      <div class="task-pay-value">
        {{ userCash }}
      </div>
    </div>

    <div class="page-part form-submit">
      <mt-button type="primary" class="form-submit" size="large"
        v-on:click="pay">
        支付
      </mt-button>
    </div>
  </div>

</template>

<script>
import axios from 'axios';
import qs from 'qs';
import { Toast } from 'mint-ui';
import storeItem from '../../utils/store';

export default {
  props: ['taskPay'],
  data() {
    return {
      userCash: 0,
    };
  },
  methods: {
    pay() {
      axios.post('/trade/pay', qs.stringify({
        trade_fare: this.taskPay.total,
        trade_num: this.taskPay.tradeNum,
        pay_way: 'sys',
        token: storeItem.getItem('token'),
      })).then((response) => {
        if (response.status === 200) {
          if (response.data.status === true) {
            Toast({
              message: response.data.message,
              duration: 1500,
            });
            setTimeout(() => {
              this.$router.push({
                name: 'Index',
              });
            }, 1500);
          } else {
            const errors = response.data.data;
            let message = '';
            Object.values(errors).forEach((element) => {
              message += element;
            });
            Toast({
              message: `${response.data.message}: ${message}`,
              duration: 1500,
            });
          }
        }
      });
    },
  },
  mounted() {
    axios.post('/user/amount', qs.stringify({
      token: storeItem.getItem('token'),
    })).then((response) => {
      if (response.status === 200 && response.data.status === true) {
        this.userCash = response.data.data.cash;
      }
    });
  },
};
</script>

<style lang="css">

.form-button {
  margin-top: 20px;
}

.form-submit {
  margin-top: 50px;
}

.task-pay-item {
  margin-top: 20px;
  padding: 20px;
  font-size: 16px;
  background-color: #f3f3f3;
}
.task-pay-title {
  display: inline-block;
  width: 40%;
  float: left;
  text-align: right;
}
.task-pay-value {
  display: inline-block;
  width: 50%;
  text-align: left;
  margin-left: 20px;
}

</style>
